/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author tim
 * http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting
 */
public class CustomerForm extends JPanel {

    private EBInterface ebi;
    private JLabel lblSearch;
    private JTextField txfSearch;
    private JTable tblCustomers;
    private JButton btnSearch;
    private JPanel buttonPanel;
    private JButton btnCreateCustomer;
    private JButton btnEditCustomer;
    private JButton btnDeleteCustomer;
    private Vector tableData;
    private TableRowSorter<CustomerTableModel> sorter;
    private CustomerTableModel model;

    public CustomerForm(EBInterface ebif) {
        ebi = ebif;
        //tableData = ebi.ebic.getAllCustomers();
        //generateComponents();

    }

     public String getSearchText() {
        return txfSearch.getText();
    }

    // gets and returns the selected customer from the table / table model
    public Object getSelectedCustomer() {
        if (tblCustomers.getSelectedRow() == -1) {
            // returns null if now row is selected
            return null;
        }
        // converts the selected row from the view to the corresponding model row
        int modelRow = tblCustomers.convertRowIndexToModel(tblCustomers.getSelectedRow());
        // gets the customer object from the model, this is not good, think about it
        return tblCustomers.getModel().getValueAt(modelRow, 6);
    }

    // sets the data for the customer table model
    public void setTableData(Vector v) {
        tableData = v;

    }

    public void createAndShowForm() {
        generateComponents();

    }

    private void generateComponents() {
        // sets system default look and feel
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }
        
        // Sets the border
        this.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Manage Customers "),
                BorderFactory.createEmptyBorder(20, 20, 20, 20)));

        this.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();



        lblSearch = new JLabel("Search:");
        // Places the GUI elements in the layout using addComponent                                  x, y, w, h, wx, wy
        addComponent(this, gbl, lblSearch, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);
        txfSearch = new JTextField(35);

        // registrates the document listener for filtering
        txfSearch.getDocument().addDocumentListener(
                new DocumentListener() {

                    public void changedUpdate(DocumentEvent e) {
                        customerFilter();
                    }

                    public void insertUpdate(DocumentEvent e) {
                        customerFilter();
                    }

                    public void removeUpdate(DocumentEvent e) {
                        customerFilter();
                    }
                });
        // Places the GUI elements in the layout using addComponent                                  x, y, w, h, wx, wy
        addComponent(this, gbl, txfSearch, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 0, 1, 1, 0, 0);

//        btnSearch = new JButton("Search");
//        btnSearch.setActionCommand("searchCustomer");
//        btnSearch.addActionListener(ebi);
//
//
//        addComponent(this, gbl, btnSearch, GridBagConstraints.NONE, GridBagConstraints.CENTER, 2, 0, 1, 1, 0, 0);

        // creates table with customer model and data
        tblCustomers = new JTable();
        // gets an instance of the customerTableModel with table data
        model = new CustomerTableModel(tableData);

        // creates the sorter for the table depending on the table model
        sorter = new TableRowSorter<CustomerTableModel>(model);

        // sets the model of tblCustomers
        tblCustomers.setModel(model);

        // adds the sorter to tblCustomers
        tblCustomers.setRowSorter(sorter);

        // allows only a single selection on the table
        tblCustomers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        

        // adds the table within a scroll pane to the layout
        // Places the GUI elements in the layout using addComponent                                                x, y, w, h, wx, wy
        addComponent(this, gbl, new JScrollPane(tblCustomers), GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0, 1, 3, 1, 1, 1);


        // adds the buttton panel to the layout
        // Places the GUI elements in the layout using addComponent                                                 x, y, w, h, wx, wy
        addComponent(this, gbl, this.createButtonPanel(), GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 2, 3, 1, 0, 0);

    }


    private void customerFilter() {
        RowFilter<CustomerTableModel, Object> rf = null;
        //If current expression doesn't parse, don't update.
        try {
            //                                              columns included in filter
            rf = RowFilter.regexFilter(txfSearch.getText(), 0, 1, 2, 3, 4, 5);

        } catch (java.util.regex.PatternSyntaxException e) {
            return;
        }
        sorter.setRowFilter(rf);
    }

    private JPanel createButtonPanel() {

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        btnCreateCustomer = new JButton("Create Customer");
        btnCreateCustomer.setActionCommand("createCustomer");
        btnCreateCustomer.addActionListener(ebi);

        btnEditCustomer = new JButton("Edit");
        btnEditCustomer.setActionCommand("editCustomer");
        btnEditCustomer.addActionListener(ebi);

        btnDeleteCustomer = new JButton("Delete");
        btnDeleteCustomer.setActionCommand("deleteCustomer");
        btnDeleteCustomer.addActionListener(ebi);

        // Sets the buttons the same size
        btnEditCustomer.setPreferredSize(btnDeleteCustomer.getPreferredSize());

        // Adds the buttons to the buttonPanel
        addComponent(buttonPanel, gbl, btnCreateCustomer, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 0, 1, 1, 0, 0);
        addComponent(buttonPanel, gbl, btnEditCustomer, GridBagConstraints.NONE, GridBagConstraints.EAST, 1, 0, 1, 1, 1, 0);
        addComponent(buttonPanel, gbl, btnDeleteCustomer, GridBagConstraints.NONE, GridBagConstraints.EAST, 2, 0, 1, 1, 0, 0);

//        buttonPanel.add(btnCreateCustomer);
//        buttonPanel.add(btnEditCustomer);
//        buttonPanel.add(btnDeleteCustomer);
        return buttonPanel;
    }

    private static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);


    }
}
